﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Nextropia.C2Manager.C2API
{
    /// <summary>
    /// represents a C2 trade (executed signal)
    /// </summary>
    public class Trade
    {
        /// <summary>
        /// the unique identifier of the trade on the C2 system
        /// </summary>
        public string ID {get;set;}

        /// <summary>
        /// the type of the security that was traded
        /// </summary>
        public SecurityType SecurityType {get;set;}

        /// <summary>
        /// the time of the first executed signal for this trade
        /// </summary>
        public DateTime EarliestOpen {get;set;}

        /// <summary>
        /// the type of the first order in this trade
        /// </summary>
        public ActionType OpenActionType {get;set;}

        /// <summary>
        /// the initial quantity of this trade
        /// </summary>
        public decimal OpenQuantity {get;set;}

        /// <summary>
        /// the symbol for which this trade was executed
        /// </summary>
        public string OpenSymbol {get;set;}

        /// <summary>
        /// the average price at which this trade was executed
        /// </summary>
        public decimal OpenPrice {get;set;}

        /// <summary>
        /// the most recent trade price of the security in this trade
        /// </summary>
        public DateTime LastClose {get;set;}

        /// <summary>
        /// the type of the order that closed this trade
        /// </summary>
        public ActionType CloseActionType {get;set;}

        /// <summary>
        /// the total quantity closed in this trade
        /// </summary>
        /// <remarks>
        /// it is possible for a trade to be partially closed
        /// </remarks>
        public decimal CloseQuantity {get;set;}

        /// <summary>
        /// the average price at which the trade was closed
        /// </summary>
        public decimal ClosePrice {get;set;}

        /// <summary>
        /// equivalent to Profit
        /// </summary>
        /// <seealso cref="Trade.Profit"/>
        public decimal Result { get; set; }
        //public decimal MaxDrawdown {get;set;}

        /// <summary>
        /// the total profit realized and unrealized in this trade
        /// </summary>
        public decimal Profit
        {
            get
            {
                //int multiplier = 1;

                //switch (SecurityType)
                //{ 
                //    case SecurityType.Forex:
                //        multiplier = 10000;
                //        break;
                //    default:
                //        multiplier = 1;
                //        break;
                //}

                //if (CloseActionType == ActionType.STC)
                //    return (CloseQuantity * ClosePrice - OpenQuantity * OpenPrice) * multiplier;
                //else
                //{
                //    return (- CloseQuantity * ClosePrice + OpenQuantity * OpenPrice) * multiplier;
                //}
                return Result;
            }
        }

        /// <summary>
        /// default constructor
        /// </summary>
        public Trade()
        {
        }

        internal Trade(XmlNode sourceNode)
        {
            ID = sourceNode.SelectSingleNode("tradeid").InnerText;
            SecurityType = (SecurityType)Enum.Parse(typeof(SecurityType), sourceNode.SelectSingleNode("type").InnerText, true);
            OpenActionType = (ActionType)Enum.Parse(typeof(ActionType), sourceNode.SelectSingleNode("openaction").InnerText);
            EarliestOpen = C2Service.SafeParseDate(sourceNode.SelectSingleNode("earliestopen").InnerText);
            OpenQuantity = C2Service.SafeParseDecimal(sourceNode.SelectSingleNode("openquant").InnerText);
            OpenSymbol = sourceNode.SelectSingleNode("opensymbol").InnerText;
            OpenPrice = C2Service.SafeParseDecimal(sourceNode.SelectSingleNode("openprice").InnerText);
            LastClose = C2Service.SafeParseDate(sourceNode.SelectSingleNode("lastclose").InnerText);
            CloseActionType = (ActionType)Enum.Parse(typeof(ActionType), sourceNode.SelectSingleNode("closeaction").InnerText);
            CloseQuantity = C2Service.SafeParseDecimal(sourceNode.SelectSingleNode("closequant").InnerText);
            ClosePrice = C2Service.SafeParseDecimal(sourceNode.SelectSingleNode("closeprice").InnerText);
            Result = C2Service.SafeParseDecimal(sourceNode.SelectSingleNode("result").InnerText);
            //MaxDrawdown = Service.SafeParseDecimal(sourceNode.SelectSingleNode("maxdrawdown").InnerText);
        }
    }
}
